package com.hrt.manage.menus.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.hrt.base.BaseDaoImpl;
import com.hrt.bean.ViewResource;
import com.hrt.manage.menus.dao.ViewResourceDao;

@Repository
public class ViewResourceDaoImpl extends BaseDaoImpl<ViewResource> implements ViewResourceDao {

	@Override
	public List<ViewResource> queryByPage(Integer scope, int pageIndex,
			int pageSize) {
		// TODO Auto-generated method stub
		List<ViewResource> results=new ArrayList<ViewResource>();
		
		final String hql="select r from ViewResource r where r.isdelete=0 and r.scope='"+scope+"'";
		
		results=this.queryByPage(hql, pageIndex, pageSize);
		return results;
	}

	@Override
	public Long totalCount(Integer scope) {
	
		Long total=0l;
		final String hql="select count(r) from ViewResource r where r.isdelete=0 and r.scope='"+scope+"'";
		
		total=this.getHibernateTemplate().execute(new HibernateCallback<Long>(){

			@Override
			public Long doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query q=session.createQuery(hql);
				return (Long) q.uniqueResult();
				
			}
			
		});
		return total;
	}

	@Override
	public ViewResource getViewResourceById(Long id) {
		
		 final String hql="select r from ViewResource r where r.isdelete=0 and r.id='"+id+"'";
		 ViewResource viewResourc=this.getHibernateTemplate().execute(
				 new HibernateCallback<ViewResource>(){

				@Override
				public ViewResource doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query q=session.createQuery(hql);
					return (ViewResource) q.uniqueResult();
				}
		 });
		 return viewResourc;
	}
	


}
